#include "InputFileReader.hh"
#include <string>
#include <iostream>

InputFileReader::InputFileReader(YAML::Node configer): m_iEntry(0)
{
    m_Chain = new TChain("SimuInfo");
    for(auto it = configer.begin(); it != configer.end(); ++it)
    {
        m_Chain->Add((it->as<std::string>()).c_str());
    }

    m_NumOfEntry = m_Chain->GetEntries();
    m_Chain->SetBranchAddress("PGTypeFlag",        &m_PGTypeFlag);
    m_Chain->SetBranchAddress("TruthEnergy",       &m_TruthEnergy);
    m_Chain->SetBranchAddress("TruthPos",          &m_TruthPos);
    m_Chain->SetBranchAddress("TruthMomDir",       &m_TruthMomDir);

    m_Chain->SetBranchAddress("NumOfOpPhoton",     &m_NumOfOpPhoton);
    m_Chain->SetBranchAddress("PhotonEnergy",      &m_PhotonEnergy);
    m_Chain->SetBranchAddress("PhotonCatchTime",   &m_PhotonCatchTime);
    m_Chain->SetBranchAddress("PhotonPos",         &m_PhotonPos);
    m_Chain->SetBranchAddress("PhotonMomDir",      &m_PhotonMomDir);
}


InputFileReader::~InputFileReader()
{
    delete m_Chain;
}

aSimEvent InputFileReader::aEvent()
{
    aSimEvent aEvent;
    if(m_iEntry >= m_NumOfEntry)
    {
        aEvent.Type = -1;
        return aEvent;
    }
    m_Chain->GetEntry(m_iEntry);

    aEvent.Type = m_PGTypeFlag;
    aEvent.Energy = m_TruthEnergy;
    aEvent.Pos = TVector3(m_TruthPos[0], m_TruthPos[1], m_TruthPos[2]);
    aEvent.MomDir = TVector3(m_TruthMomDir[0], m_TruthMomDir[1], m_TruthMomDir[2]);
    aEvent.VSimPhoton.clear();
    for(Int_t i = 0; i < m_NumOfOpPhoton; ++i)
    {
        aSimPhoton aPhoton;
        aPhoton.Energy = m_PhotonEnergy[i];
        aPhoton.CatchTime = m_PhotonCatchTime[i];
        aPhoton.Pos = TVector3(m_PhotonPos[i][0], m_PhotonPos[i][1], m_PhotonPos[i][2]);
        aPhoton.MomDir = TVector3(m_PhotonMomDir[i][0], m_PhotonMomDir[i][1], m_PhotonMomDir[i][2]);
        aEvent.VSimPhoton.push_back(aPhoton);
    }
    ++m_iEntry;
    return aEvent;
}